<template>
  <div class="elm-edit f_edit">
    <elm-page-bb class="ei_edit" v-loading="loading">

      <el-card v-if="all_node_num>2" shadow="hover" style="margin-top: 10px;height: 265px;">
        <div slot="header">
          <span style="font-weight: bold;">所有传出连线</span>
        </div>
        <el-row style="text-align: right;height: 35px;padding: 3px;">
          <el-button size="mini" @click="addLine">新增</el-button>
        </el-row>
        <elm-table :data='line_from_node_list' :stripe='true' :border='true' size='mini' :page-size="9999" style="height: 175px;">
          <el-table-column label="指向节点" min-width="100px" header-align="center">
            <template slot-scope="scope">
              <el-select v-model="scope.row.end_node" size="mini" style="width:120px;" :disabled="!scope.row.allow_change_to_node">
                <el-option
                    v-for="item in node_for_select_list"
                    :key="item.id"
                    :label="item.title"
                    :value="item.id">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="通过条件" min-width="200px" header-align="center">
            <template slot-scope="scope">
              {{scope.row.condition_s_str}}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="60px" fixed="right" align="center">
            <template slot-scope="scope">
              <el-button @click="deleteLine(scope.row.id)" type="text" size="mini" :disabled="!scope.row.allow_delete">
                删除</el-button>
            </template>
          </el-table-column>
        </elm-table>
      </el-card>

      <template #bottom>
        <el-button type="success" size="small" @click="submit">立即保存</el-button>
        <el-button size="small" @click="close">取消</el-button>
      </template>
    </elm-page-bb>
  </div>
</template>

<script>
export default {
  extends: elmVueBase,
  data() {
    return {
      loading:true,

      DE_INFO: null,

      node_type:'',
      node_id:'',
      node_title:'',

      line_from_node_list:[],
      node_for_select_list:[],
      all_node_num:0,

      add_line_list:[],
      delete_line_id_list:[],

      flow_param_list:[],
    }
  },
  mounted() {
    this.DE_INFO = elm.dialog.request;
    this.node_type = this.DE_INFO.data.node_type;
    this.node_id = this.DE_INFO.data.node_id;
    this.node_title = this.DE_INFO.data.node_title;
    this.all_node_num = this.DE_INFO.line_data.all_node_num;
    this.line_from_node_list = this.DE_INFO.line_data.line_from_node_list;
    this.node_for_select_list = this.DE_INFO.line_data.node_for_select_list;
    this.flow_param_list = this.DE_INFO.line_data.flow_param_list;

    this.v_getData();
  },
  methods: {
    async v_getData(){
      this.loading = true;
      this.checkLineList();
      this.loading = false;
    },
    checkLineList:function (){
      for(let i=0;i<this.line_from_node_list.length;i++){
        let item = this.line_from_node_list[i];
        item['condition_s_str'] = '无';
        if(!this.em(item.condition_s)){
          item['condition_s_str'] = item.condition_s;
        }
      }
      this.line_from_node_list = JSON.parse(JSON.stringify(this.line_from_node_list));
    },

    addLine:function (){
      let lineData = {
        'id': elm.newGuid(),
        'start_node': this.node_id,
        'type':'default',
        'allow_change_to_node':true,
        'allow_delete':true,
        'condition_s':'',
        'condition_s_str':'',
      };
      this.add_line_list.push(lineData);
      this.line_from_node_list.push(lineData);
    },
    deleteLine:function (line_id){
      let newList = [];
      for(let i=0;i<this.line_from_node_list.length;i++){
        if(this.line_from_node_list[i].id!=line_id){newList.push(this.line_from_node_list[i]);}
      }
      this.line_from_node_list = newList;
      this.delete_line_id_list.push(line_id);
    },

    submit:function (){
      //保存时要求所有连线均有指向，且指向的节点不能重复
      let checkData = {};
      for(let i=0;i<this.line_from_node_list.length;i++){
        let lineData = this.line_from_node_list[i];
        if(this.em(lineData.end_node)){
          this.$message.error('连线指向不能为空');
          return;
        }
        if(checkData[lineData.end_node]){
          this.$message.error('连线指向的节点不能重复');
          return;
        }
        checkData[lineData.end_node] = true;
      }

      let pData = {
        'add_line_list':this.add_line_list,
        'delete_line_id_list':this.delete_line_id_list,
        'refresh_line_list':this.line_from_node_list
      };
      this.DE_INFO.callVue.refreshNodeLine(pData);
      this.v_cancle();
    },
    v_cancle(){
      this.close();//extends: elmVueBase
    },

    em(s) {
      if (s == null || s == '') { return true; }
      else { return false; }
    },

  }
}
</script>
<style>
.f_edit .elm-body{ padding: 5px; }
</style>